﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("OpenPhotoAlbum.Controls");

OpenPhotoAlbum.Controls.Paginator = function(element) {
	OpenPhotoAlbum.Controls.Paginator.initializeBase(this, [element]);

	this._PageFormat = null;
	this._PreviousPageText = null;
	this._NextPageText = null;
	this._TotalPages = null;

	this._currentPage = 1;

	this._previousPageElement = null;
	this._pageInfoElement = null;
	this._nextPageElement = null;
}

OpenPhotoAlbum.Controls.Paginator.prototype = {
	initialize: function() {
		OpenPhotoAlbum.Controls.Paginator.callBaseMethod(this, 'initialize');

		var previousPageBtnId = this._element.id + "_previousPage";
		var pageInfoId = this._element.id + "_pageInfo";
		var nextPageBtnId = this._element.id + "_nextPage";

		var html = '';
		html += String.format('<a id="{0}" href="javascript:void(0);" style="display:none;">{1}</a>', previousPageBtnId, this._PreviousPageText);
		html += String.format('<span id="{0}" class="pgnText"></span>', pageInfoId);
		html += String.format('<a id="{0}" href="javascript:void(0);" style="display:none;">{1}</a>', nextPageBtnId, this._NextPageText);
		this._element.innerHTML = html;

		this._previousPageElement = $get(previousPageBtnId);
		this._pageInfoElement = $get(pageInfoId);
		this._nextPageElement = $get(nextPageBtnId);

		this.renderPageInfo();
		this.renderButtons;

		$addHandler(this._previousPageElement, "click", Function.createDelegate(this, this.previousPage));
		$addHandler(this._nextPageElement, "click", Function.createDelegate(this, this.nextPage));
	},
	dispose: function() {
		$clearHandlers(this._previousPageElement);
		$clearHandlers(this._nextPageElement);

		OpenPhotoAlbum.Controls.Paginator.callBaseMethod(this, 'dispose');
	},

	_raisePageIndexChangedEvent: function() {
		if (this._currentPage > this._TotalPages) { this._currentPage = this._TotalPages; }
		if (this._currentPage < 1) { this._currentPage = 1; }

		this.renderPageInfo();
		this.renderButtons();

		var eventArgs = new Sys.EventArgs();
		eventArgs.pageIndex = this._currentPage;
		this._raiseEvent('PageIndexChanged', eventArgs);
	},

	previousPage: function() {
		if (this._currentPage == 1) { return; }
		this._currentPage--;
		this._raisePageIndexChangedEvent();
	},

	nextPage: function() {
		if (this._currentPage == this._TotalPages) { return; }
		this._currentPage++;
		this._raisePageIndexChangedEvent();
	},

	renderPageInfo: function() {
		if (!this._TotalPages || !this._pageInfoElement) { return; }

		var pageInfoText = String.format(this._PageFormat, this._currentPage, this._TotalPages);
		this._pageInfoElement.innerHTML = pageInfoText;
	},

	renderButtons: function() {
		if (this._currentPage == 1) { this._previousPageElement.className = 'pgnBtnDisabled'; }
		else { this._previousPageElement.className = 'pgnBtnEnabled'; }

		if (this._currentPage == this._TotalPages) { this._nextPageElement.className = 'pgnBtnDisabled'; }
		else { this._nextPageElement.className = 'pgnBtnEnabled'; }

		this._previousPageElement.style.display = '';
		this._nextPageElement.style.display = '';
	},

	updateCurrentPage: function(value) {
		if (value == null || value < 1 || value > this._TotalPages) { return; }

		this._currentPage = value;
	},

	get_Text: function() { return this._Text; },
	set_Text: function(value) { this._Text = value; },

	get_IsEditing: function() { return this._IsEditing; },
	set_IsEditing: function(value) { this._IsEditing = value; },

	add_PageIndexChanged: function(handler) { this.get_events().addHandler('PageIndexChanged', handler); },
	remove_PageIndexChanged: function(handler) { this.get_events().removeHandler('PageIndexChanged', handler); },

	_raiseEvent: function(eventName, eventArgs) {
		var handler = this.get_events().getHandler(eventName);
		if (handler) {
			if (!eventArgs) { eventArgs = Sys.EventArgs.Empty; }
			handler(this, eventArgs);
		}
	},

	get_PageFormat: function() { return this._PageFormat; },
	set_PageFormat: function(value) { this._PageFormat = value; },

	get_PreviousPageText: function() { return this._PreviousPageText; },
	set_PreviousPageText: function(value) { this._PreviousPageText = value; },

	get_NextPageText: function() { return this._NextPageText; },
	set_NextPageText: function(value) { this._NextPageText = value; },

	get_TotalPages: function() { return this._TotalPages; },
	set_TotalPages: function(value) { this._TotalPages = value; }
}
OpenPhotoAlbum.Controls.Paginator.registerClass('OpenPhotoAlbum.Controls.Paginator', AjaxControlToolkit.ControlBase);
